﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
using Clandestine.Battle;
using Clandestine.Battle.Enemies;
using Clandestine.Conversations;
using Clandestine.Battle.Effects;
using OpenTK;

namespace Clandestine
{
    public static class Program
    {
        public static bool Running = true;

        public static Thread BattleThread;

        public static void WorkingDirectoryFixUp()
        {
            string gcd = Directory.GetCurrentDirectory();
            if (gcd.Substring(gcd.Length - 6, 6).ToLower().Contains("datas"))
                return; // probably in the correct folder already

            if (Directory.Exists("../datas"))
                Directory.SetCurrentDirectory("../datas");
            else if (Directory.Exists("../../datas"))
                Directory.SetCurrentDirectory("../../datas");
            else if (Directory.Exists("../../Clandestine/datas"))
            {
                Directory.SetCurrentDirectory("../../Clandestine/datas");
                Log.i("Datas folder not in current dir, or one or two folders up"
                    + " - but ../../Clandestine/datas/ was found, "
                    + "so we'll use that!");
            }
            else if (Directory.Exists("../../../Clandestine/datas"))
            {
                Directory.SetCurrentDirectory("../../../Clandestine/datas");
                Log.i("Datas folder not in current dir, or one or two folders up"
                    + " - but ../../../Clandestine/datas/ was found, "
                    + "so we'll use that!");
            }
            else
            {
                MessageBox.Show("Ah. Can't find the datas! Either in ../datas or ../../datas, please!", "Fail", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
        }

        static void Main(string[] args)
        {
            WorkingDirectoryFixUp();
		
            Graphics.Initialise(1, new OpenTkManager());
            Keyboard.Initialise();
            Fonts.Initialise();
            //Ticker.Initialise();
            Graphics.SetWindowTitle("Project Clandestine.");

            BattleEffects.LoadEffects();
            //initSampleBattle();

            WorldMap.Start();
            WorldMap.Player = new MapPlayer();
            //WorldMap.Level = Level.GetLevel("house.xml");
            WorldMap.Level = Level.GetLevel("rpgmaker/exterior.xml");
            WorldMap.Player.X = 900;
            WorldMap.Player.Y = 700;
           // WorldMap.Player.Layer = 0;

            // Debug
            Conversation.Conversations.Add("farmer_1", new Conversation("conversations/Conversation.xml"));
            Conversation.Conversations.Add("truestory", new Conversation("conversations/truestory.xml"));
            Conversation.Conversations.Add("Demo", new Conversation("conversations/demo.xml"));
            Conversation.Conversations.Add("feeling_better", new Conversation("conversations/feelingbetter.xml"));

            // main loop goes here, never leaves
            // (if you close the window, graphics has a callback func that sets Running to false also)
            /*System.Timers.Timer timer = new System.Timers.Timer(15);
            timer.Elapsed += new System.Timers.ElapsedEventHandler(markitok);
            timer.AutoReset = true;
            timer.Start();

            progMainLoop();

            timer.Stop();*/

            GameWindow gw = ((OpenTkManager)Graphics.Manager).GameWindow;
            gw.RenderFrame += new EventHandler<FrameEventArgs>(gw_RenderFrame);
            gw.Run(120, 120);

            //Ticker.Deinitialise();
            if (BattleThread != null)
                BattleThread.Abort();
            WorldMap.Stop();
            Graphics.Deinitialise();
            Keyboard.Deinitialise();
        }

        static void gw_RenderFrame(object sender, FrameEventArgs e)
        {
            Ticker.DoOneTick();
            Graphics.Render();
        }

        private static int z = 0;
        private static bool cango = false;

        private static void markitok(object o, System.Timers.ElapsedEventArgs eea)
        {
            cango = true;
        }

        private static void progMainLoop()
        {
            while (Running)
            {
                while (!cango)
                    Thread.Sleep(1);
                cango = false;

                DateTime start = DateTime.Now;
                Ticker.DoOneTick();
                Graphics.Render();
                DateTime end = DateTime.Now;

                z++;
                if (z == 30)
                {
                    Log.i((end - start).TotalMilliseconds.ToString());
                    z = 0;
                }

                if (Keyboard.IsPressed(Key.Quit))
                    Running = false;  
            }
        }
    }
}
